﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SlaktDataReader
{
    public class DeathBookTextFile : TextFileBase
    {
        //private Dictionary<string, int> _fields = new Dictionary<string, int>();

        public List<DeathBookRecord> Records { get; set; }

        public DeathBookTextFile()
        {
            _fields.Add("Lopnr", -1);
            _fields.Add("Sidnr", -1);
            _fields.Add("Ordnr", -1);
            _fields.Add("Ddatum", -1);
            _fields.Add("Bdatum", -1);
            _fields.Add("Kon", -1);
            _fields.Add("Titel", -1);
            _fields.Add("Fnamn", -1);
            _fields.Add("Enamn", -1);
            _fields.Add("Adress", -1);
            _fields.Add("Nadress", -1);
            _fields.Add("Dalder", -1);
            _fields.Add("Dorsak", -1);
            _fields.Add("Fdatum", -1);
            _fields.Add("Fadress", -1);
            _fields.Add("Nfadress", -1);
            _fields.Add("Anhorig1", -1);
            _fields.Add("Anhorig2", -1);
            _fields.Add("Ovr1", -1);
            _fields.Add("Ovr2", -1);
            _fields.Add("Ovr3", -1);
            _fields.Add("Mtitel", -1);
            _fields.Add("Mfnamn", -1);
            _fields.Add("Menamn", -1);
            _fields.Add("Ktitel", -1);
            _fields.Add("Kfnamn", -1);
            _fields.Add("Kenamn", -1);
            _fields.Add("Uppdaterat", -1);

            Separator = '\t';
            Records = new List<DeathBookRecord>();
            FileEncoding = Encoding.UTF7;
        }



        protected override void _AddRecord(string[] RowFields)
        {
            if (RowFields.Length > 0)
            {
                DeathBookRecord r = new DeathBookRecord();
                for (int i = 0; i < RowFields.Length; i++)
                {
                    if (_fields.ContainsValue(i))
                    {
                        try
                        {
                            switch (_fields.Where(f => f.Value == i).First().Key)
                            {
                                case "Lopnr":
                                    try
                                    {
                                        r.RowID = Convert.ToInt32(RowFields[i]);
                                    }
                                    catch
                                    {
                                        r.RowID = 0;
                                    }
                                    break;
                                case "Sidnr":
                                    r.PageNo = RowFields[i];
                                    break;
                                case "Ordnr":
                                    r.OrdNo = RowFields[i];
                                    break;
                                case "Ddatum":
                                    r.DeadDate = RowFields[i];
                                    break;
                                case "Bdatum":
                                    r.BurialDate = RowFields[i];
                                    break;
                                case "Kon":
                                    try
                                    {
                                        if (!string.IsNullOrWhiteSpace(RowFields[i]))
                                        {
                                            r.Gender = Convert.ToChar(RowFields[i]);
                                        }
                                        else
                                        {
                                            r.Gender = 'O';
                                        }
                                    }
                                    catch
                                    {
                                        r.Gender = 'O';
                                    }
                                    break;
                                case "Titel":
                                    r.Title = RowFields[i];
                                    break;
                                case "Fnamn":
                                    r.FirstName = RowFields[i];
                                    break;
                                case "Enamn":
                                    r.SurName = RowFields[i];
                                    break;
                                case "Adress":
                                    r.Address = RowFields[i];
                                    break;
                                case "Nadress":
                                    r.NAddress = RowFields[i];
                                    break;
                                case "Dalder":
                                    r.DeadAge = RowFields[i];
                                    break;
                                case "Dorsak":
                                    r.DeadReason = RowFields[i];
                                    break;
                                case "Fdatum":
                                    r.BirthDate = RowFields[i];
                                    break;
                                case "Fadress":
                                    r.BornAddress = RowFields[i];
                                    break;
                                case "Nfadress":
                                    r.NBornAddress = RowFields[i];
                                    break;
                                case "Anhorig1":
                                    r.Related1 = RowFields[i];
                                    break;
                                case "Anhorig2":
                                    r.Related2 = RowFields[i];
                                    break;
                                case "Ovr1":
                                    r.Extra1 = RowFields[i];
                                    break;
                                case "Ovr2":
                                    r.Extra2 = RowFields[i];
                                    break;
                                case "Ovr3":
                                    r.Extra3 = RowFields[i];
                                    break;
                                case "Mtitel":
                                    r.MTitle = RowFields[i];
                                    break;
                                case "Mfnamn":
                                    r.MFirstName = RowFields[i];
                                    break;
                                case "Menamn":
                                    r.MSurName = RowFields[i];
                                    break;
                                case "Ktitel":
                                    r.KTitle = RowFields[i];
                                    break;
                                case "Kfnamn":
                                    r.KFirstName = RowFields[i];
                                    break;
                                case "Kenamn":
                                    r.KSurName = RowFields[i];
                                    break;
                                case "Uppdaterat":
                                    r.Updated = RowFields[i];
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (Exception ex) { /*System.Windows.MessageBox.Show(ex.Message); */ }
                    }
                }
                if (r.IsChanged)
                {
                    r.IsChanged = false;
                    Records.Add(r);
                }
            }
        }

        public bool Write(string FileName)
        {
            this.FileName = FileName;
            return Write();
        }

        public bool Write()
        {
            return false;
        }
    }
}
